<?php
class Model_Admin_Banner extends Model{
    public function getInfo(){

        $sql="select t.bannerid id,t.description,t.url,t.image,t.sort,t.isdisabled,t.createdon,t.createdby
             from banner t
             where isdeleted<>'1'
             order by t.CreatedOn desc";

        $data=DB::query(Database::SELECT,$sql)->execute()->as_array();

        return $data;
    }
    public  function saveInfo($id,$description,$url,$image,$sort,$isdisabled){

        if($id==""){
            return $this->add($description,$url,$image,$sort,$isdisabled);
        }
        else{
            return $this->alter($id,$description,$url,$image,$sort,$isdisabled);
        }

    }
    private function add($description,$url,$image,$sort,$isdisabled){
        try {

            list($insert_id, $total_rows)= DB::insert('banner')

            ->columns(array('description','image','url','isdisabled','CreatedBy','CreatedOn'))
                
            ->values(array($description,$image,$url,$isdisabled,Session::instance()

            ->get('admin_username',NULL),date("Y-m-d h:i:sa")))->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows,"id"=>$insert_id));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
    private function alter($id,$description,$url,$image,$sort,$isdisabled){

        try {
            $total_rows=DB::update('banner')

            ->set(array('description'=>$description,'url'=>$url,'image'=>$image,'sort'=>$sort,'isdisabled'=>$isdisabled))

            ->where('bannerid','=',$id)->execute();

            Database::instance()->commit();

            return json_encode(array("info"=>"success","count"=>$total_rows));
        }
        catch (Exception $e) {

            Database::instance()->rollback();

            return json_encode(array("info"=>"服务器错误，保存失败，请联系管理员或者重试"));
        }
    }
}